<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12" >
          <el-form-item label="所属模块:" prop="tmBasModuleId">
            <el-select v-model="formModel.tmBasModuleId" disabled clearable filterable>
              <el-option v-for="item in moduleOptions" :key="item.tmBasModuleId" :label="item.moduleCode+'-'+item.moduleDesc" :value="item.tmBasModuleId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="识别码:" prop="markCode">
            <el-input v-model="formModel.markCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段代码:" prop="filedCode">
            <el-input v-model="formModel.filedCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段描述:" prop="filedDesc">
            <el-input v-model="formModel.filedDesc" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段类型:" prop="filedType">
            <hey-select v-model="formModel.filedType" disabled clcd="Key_ECU_Module_Filed" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-show="!showFile">
          <el-form-item label="字段值:" prop="filedValue">
            <el-input v-model="formModel.filedValue" :disabled="disabledValue" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-show="showFile">
          <el-form-item label="字段值:" prop="filedValue">
            <el-select v-model="formModel.filedValue" :disabled="disabledValue" clearable filterable>
              <el-option v-for="item in fileOptions" :key="item.fileId" :label="item.fileName" :value="item.fileName"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="EW审核信息:" prop="ewAuditInfo">
            <el-input v-model="formModel.ewAuditInfo" :disabled="disabledEw" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showMw">
          <el-form-item label="MW审核信息:" prop="ewAuditInfo">
            <el-input v-model="formModel.mwAuditInfo"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'ModuleBscInfoAudit',
  props: ['pageType', 'editData'],
  data() {
    return {
      moduleOptions:[],
      fileOptions:[],
      disabledValue:false,
      disabledEw:false,
      showMw:false,
      showFile:false,
      formModel: {
        rcrdId: '',
        tmBasModuleId: '',
        trBasModuleFiledId: '',
        filedCode: '',
        filedType: '',
        filedDesc: '',
        markCode: '',
        filedValue: '',
        ewAuditInfo: '',
        mwAuditInfo: '',
      },
      rules: {
      }
    }
  },
  computed: {

  },
  mounted() {
    this.getModule()
    this.init()
  },
  methods: {
    getModule(){
      const param = {pageNum:1,pageSize:9999}
      param.isDelete = '0'
      this.get('/tmBasModule/queryTmBasModule', param).then(res => {
        this.moduleOptions = res.list
      })
    },
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      this.assignData(editData, formModel)
      if(formModel.filedType=='1'){
        //如果字段类型为1:文件
        //通过模块ID查询电检模块FTP文件
        const param = {pageNum:1,pageSize:9999}
        param.isDelete = '0'
        param.tmBasModuleId = formModel.tmBasModuleId
        this.get('/trBasEcuFtpFile/queryTrBasEcuFtpFile', param).then(res => {
          this.fileOptions = res.list
          this.showFile = true
        })
      }else{
        //如果字段类型为0:值
        this.showFile = false
      }
      if (pageType == 'add') {
        //版本新增
        formModel.filedValue = ''
        formModel.ewAuditInfo = ''
        formModel.mwAuditInfo = ''
        this.disabledValue = false
        this.disabledEw = false
        this.showMw = false
      }else if(pageType == 'ewAudit'){
        //ew审核
        this.disabledValue = false
        this.disabledEw = false
        this.showMw = false
      }else if(pageType == 'mwAudit'){
        //mw审核
        this.disabledValue = true
        this.disabledEw = true
        this.showMw = true
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        tmBasModuleId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        filedCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        filedType: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        filedDesc: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        markCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        filedValue: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        ewAuditInfo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        mwAuditInfo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { pageType, formModel } = this
          const param = { ...formModel }
          var operateType = ''
          if (pageType == 'add') {
            //版本新增
            operateType = '08'
          }else if(pageType == 'ewAudit'){
            //ew审核
            operateType = '06'
          }else if(pageType == 'mwAudit'){
            //mw审核
            operateType = '07'
          }
          param.operateType = operateType
          const url = '/trBasModuleFiledAudit/maintainTrBasModuleFiledAudit'
          const tip = '操作成功!'
          if(operateType=='08'){
            this.$sure('新增字段版本将会导致工程号与模块中该模块该识别码下所有数据发布状态也一起置为[未发布],需要重新发布,确定新增?', _ => {
              this.post(url, param).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            })
          }else{
            this.post(url, param).then(res => {
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }

        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
